package com.fyp.speachrecognition;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class Dialer extends Activity {

	
	EditText number;
	private final String LOG_TAG = "Speech Dialer";
	String Dial="tel:";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialer);
		Button a = (Button) findViewById(R.id.recordNamePromptForSave);
		Button b = (Button) findViewById(R.id.playNoSpeak);
		Button c = (Button) findViewById(R.id.recordSavingMenu);
		Button d = (Button) findViewById(R.id.playSavingMenu);
		Button e = (Button) findViewById(R.id.recordPasswordPrompt);
		Button f = (Button) findViewById(R.id.playPassPrompt);
		Button g = (Button) findViewById(R.id.button7);
		Button h = (Button) findViewById(R.id.button8);
		Button i = (Button) findViewById(R.id.button9);
		Button j = (Button) findViewById(R.id.button10);
		Button Star = (Button) findViewById(R.id.button11);
		Button Hash = (Button) findViewById(R.id.button12);
		Button clear = (Button) findViewById(R.id.button13);
		Button call = (Button) findViewById(R.id.button14);
		number =  (EditText) findViewById(R.id.numberTextFromDialer);
		
		PhoneCallListener phoneListener = new PhoneCallListener();
		TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
		telephonyManager.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);
		
		a.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setNumer("1");
				MediaPlayer mp = MediaPlayer.create( Dialer.this,R.raw.a);
				mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
					
					@Override
					public void onCompletion(MediaPlayer mp) {
						// TODO Auto-generated method stub
						mp.stop();
						mp.release();
					}
				});
			
				
			}
			
		});
		
		b.setOnClickListener(new OnClickListener(){
			

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setNumer("2");
				MediaPlayer mp = MediaPlayer.create( Dialer.this,R.raw.b);
				mp.start();
				mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
					
					@Override
					public void onCompletion(MediaPlayer mp) {
						// TODO Auto-generated method stub
						mp.stop();
						mp.release();
					}
				});
				
			}
		});
		c.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setNumer("3");
				MediaPlayer mp = MediaPlayer.create( Dialer.this,R.raw.c);
				mp.start();
			
				mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
					
					@Override
					public void onCompletion(MediaPlayer mp) {
						// TODO Auto-generated method stub
						mp.stop();
						mp.release();
					}
				});
			}
			
		});
		d.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setNumer("4");
				MediaPlayer mp = MediaPlayer.create( Dialer.this,R.raw.d);
				mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
					
					@Override
					public void onCompletion(MediaPlayer mp) {
						// TODO Auto-generated method stub
						mp.stop();
						mp.release();
					}
				});
				
			}
			
		});
		
		e.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setNumer("5");
				MediaPlayer mp = MediaPlayer.create( Dialer.this,R.raw.e);
				mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
					
					@Override
					public void onCompletion(MediaPlayer mp) {
						// TODO Auto-generated method stub
						mp.stop();
						mp.release();
					}
				});
				
			}
			
		});
		f.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setNumer("6");
				MediaPlayer mp = MediaPlayer.create( Dialer.this,R.raw.f);
				mp.start();
			
				
			}
			
		});
		g.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setNumer("7");
				MediaPlayer mp = MediaPlayer.create( Dialer.	this,R.raw.g);
				mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
					
					@Override
					public void onCompletion(MediaPlayer mp) {
						// TODO Auto-generated method stub
						mp.stop();
						mp.release();
					}
				});
				
			}
			
		});
		h.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setNumer("8");
				MediaPlayer mp = MediaPlayer.create( Dialer.this,R.raw.h);
				mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
					
					@Override
					public void onCompletion(MediaPlayer mp) {
						// TODO Auto-generated method stub
						mp.stop();
						mp.release();
					}
				});
				
			}
			
		});
		i.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setNumer("9");
				MediaPlayer mp = MediaPlayer.create( Dialer.this,R.raw.i);
				mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
					
					@Override
					public void onCompletion(MediaPlayer mp) {
						// TODO Auto-generated method stub
						mp.stop();
						mp.release();
					}
				});
				
			}
			
		});
		j.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setNumer("0");
				MediaPlayer mp = MediaPlayer.create( Dialer.this,R.raw.j);
				mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
					
					@Override
					public void onCompletion(MediaPlayer mp) {
						// TODO Auto-generated method stub
						mp.stop();
						mp.release();
					}
				});
				
			}
			
		});
		Star.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setNumer("*");
				MediaPlayer mp = MediaPlayer.create( Dialer.this,R.raw.staric);
				mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
					
					@Override
					public void onCompletion(MediaPlayer mp) {
						// TODO Auto-generated method stub
						mp.stop();
						mp.release();
					}
				});
				
			
				
			}
			
		});
		Hash.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setNumer("#");
				MediaPlayer mp = MediaPlayer.create( Dialer.this,R.raw.hash);
				mp.start();
				
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
					
					@Override
					public void onCompletion(MediaPlayer mp) {
						// TODO Auto-generated method stub
						mp.stop();
						mp.release();
					}
				});
			
				
			}
			
		});
		clear.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				number.setText("");
				
			
				
			}
			
		});
		call.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(number.getText().length()>0){
				Dial=Dial+number.getText();
			
				Log.v(LOG_TAG, "Dial: "+Dial);
	        	Intent callIntent = new Intent(Intent.ACTION_CALL);
				callIntent.setData(Uri.parse(Dial));
				startActivity(callIntent);
				Dial="tel:";
				}
				else
				{
					Toast.makeText(Dialer.this, "Write No for Dial", Toast.LENGTH_SHORT).show();
				}
			}
			
		});
		
		
		
		
	}
	private void setNumer(String s)
	{
		number.setText(number.getText()+s);
	}
	

	private class PhoneCallListener extends PhoneStateListener {
		 
		private boolean isPhoneCalling = false;
	
		String LOG_TAG = "LOGGING 123";
	
		@Override
		public void onCallStateChanged(int state, String incomingNumber) {
	
			if (TelephonyManager.CALL_STATE_RINGING == state) {
				// phone ringing
				Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
			}
	
			if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
				// active
				Log.i(LOG_TAG, "OFFHOOK");
	
				isPhoneCalling = true;
			}
	
			if (TelephonyManager.CALL_STATE_IDLE == state) {
				// run when class initial and phone call ended, 
				// need detect flag from CALL_STATE_OFFHOOK
				Log.i(LOG_TAG, "IDLE");
	
				if (isPhoneCalling) {
	
					Log.i(LOG_TAG, "restart app");
	
					Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
					i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(i);
	
					isPhoneCalling = false;
				}
	
			}
		}
	}
	
}


